

package yf.mall.service.impl;

import org.springframework.transaction.annotation.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.excel.EasyExcel;
import yf.mall.enums.LotteryGoodsStatusEnum;
import yf.mall.vo.down.LotteryGoodsDown;

import java.util.List;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletResponse;
import java.util.stream.Collectors;

import yf.mall.dao.LotteryGoodsDao;
import yf.mall.entity.LotteryGoodsEntity;
import yf.mall.vo.LotteryGoodsVo;
import yf.mall.service.LotteryGoodsService;
import com.yf.service.impl.BaseServiceImpl;

/**
 * 账户记录Service实现类
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Thu Aug 20 11:18:45 CST 2020
 */
@Service
public class    LotteryGoodsServiceImpl extends BaseServiceImpl<LotteryGoodsEntity> implements LotteryGoodsService {

    private LotteryGoodsDao dao;


    @Autowired
    public void setDao(LotteryGoodsDao dao) {
        this.dao = dao;
        super.setBaseDao(dao);
    }


    @Override
    public List<LotteryGoodsVo> findVo(LotteryGoodsVo vo) {
        return dao.findVo(vo);
    }

    @Override
    public LotteryGoodsVo getVo(Long id) {
        return dao.getVo(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveVo(LotteryGoodsVo vo) {
        return dao.save(vo.getEntity());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateVo(LotteryGoodsVo vo) {
        return dao.update(vo.getEntity());
    }


    @Override
    public void downLoad(HttpServletResponse response, List<LotteryGoodsVo> list) {
        try {
            List<LotteryGoodsDown> datas = list.stream().map(item -> new LotteryGoodsDown(item)).collect(Collectors.toList());
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("测试", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), LotteryGoodsDown.class).sheet("模板").doWrite(datas);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    /**
     * 内部方法
     **/


    @Override
    public List<LotteryGoodsEntity> findByActId(Long id, int status) {
        return dao.findByActId(id, status);
    }

    @Override
    public LotteryGoodsEntity findMaxGoods(Long actId) {
        return dao.findMaxGoods(actId);
    }
    @Override
    public LotteryGoodsEntity findMinGoods(Long actId) {
        return dao.findMinGoods(actId);
    }

    @Override
    public List<LotteryGoodsEntity> findByActIdAndStatus(Long actId, int status) {
        return dao.findByActIdAndStatus(actId, status);
    }


}
